<!DOCTYPE html>
<html>

<head>
    {{template "header"}}
</head>

<body>
<script>
    let tableid = webix.uid()
    let deleteItem = function (ids, callback) {
        webix.confirm({
            title: "Operation confirmation",
            ok: "Yes", cancel: "No",
            text: "Confirm to delete? This operation is irreversible.",
            callback: function (ev) {
                if (ev) {
                    webix.ajax().get('/admin/cwmp/config/session/delete', {ids: ids}).then(function (result) {
                        let resp = result.json();
                        webix.message({type: resp.msgtype, text: resp.msg, expire: 2000});
                        if (callback)
                            callback()
                    }).fail(function (xhr) {
                        webix.message({type: 'error', text: "Delete Failure:" + xhr.statusText, expire: 2000});
                    });
                }
            }
        });
    }

    webix.ready(function () {
        let queryid = webix.uid().toString()
        let tableid = webix.uid().toString();
        let reloadData = wxui.reloadDataFunc(tableid, "/admin/cwmp/config/session/query", queryid)
        let showLog = function (id, node) {
            let ditem = $$(tableid).getItem(id)
            webix.ui({
                view: "popup", height: 400, width: 800, scroll: "auto", body: {
                    name: "content", view: "codemirror-editor", mode: "python", value: ditem.last_error
                }
            }).show(node)
        }
        webix.ui({
            css: "main-panel",
            padding: 7,
            rows: [
                wxui.getPageToolbar({
                    title: tr("cwmp", "Tr069 config session"),
                    icon: "mdi mdi-cast-education",
                    elements: [
                        wxui.getDangerButton(gtr("Remove"), 90, false, function () {
                            let rows = wxui.getTableCheckedIds(tableid);
                            if (rows.length === 0) {
                                webix.message({type: 'error', text: "Please select one", expire: 1500});
                            } else {
                                deleteItem(rows.join(","), reloadData);
                            }
                        }),
                    ]
                }),
                wxui.getTableQueryCustomForm(queryid, [
                    {
                        cols: [
                            {
                                view: "datepicker",
                                timepicker: true,
                                name: "starttime",
                                label: gtr("Time From"),
                                labelWidth: 80,
                                width: 240,
                                stringResult: true,
                                format: "%Y-%m-%d %H:%i",
                                css: "nborder-input",
                                value: webix.Date.add(new Date(), -8, "hour"),
                                editable: true
                            },
                            {
                                view: "datepicker",
                                timepicker: true,
                                name: "endtime",
                                label: gtr("to"),
                                labelWidth: 20,
                                stringResult: true,
                                format: "%Y-%m-%d %H:%i",
                                css: "nborder-input",
                                value: new Date(),
                                editable: true
                            },
                            {view: "search", id: "keyword", name: "keyword", placeholder: "keywords", width: 320},
                            {
                                view: "button",
                                label: "Query",
                                css: "webix_transparent",
                                type: "icon",
                                icon: "mdi mdi-search-web",
                                borderless: true,
                                width: 100,
                                click: function () {
                                    reloadData()
                                }
                            }, {}
                        ]
                    }
                ]),
                wxui.getDatatable({
                    tableid: tableid,
                    url: '/admin/cwmp/config/session/query',
                    columns: [
                        {
                            id: "state",
                            header: {content: "masterCheckbox", css: "center"},
                            headermenu: false,
                            adjust: true,
                            css: "center",
                            template: "{common.checkbox()}"
                        },
                        {
                            id: "exec_status",
                            header: [tr("cwmp", "Status")],
                            template: "<a class='do_log_detail' href='javascript:void(0)'><i class='mdi mdi-eye statuscss_#exec_status#' ></i></a> <span class='statuscss_#exec_status#'>#exec_status#</span>",
                            adjust: true,
                            sort: "server",
                        },
                        {id: "cpe_id", options: "/admin/cpe/options", header: [tr("cwmp", "CPE")], adjust: true},
                        {
                            id: "name",
                            header: [tr("cwmp", "Name")],
                            adjust: true,
                            sort: "server",
                        },
                        {
                            id: "level",
                            header: [tr("cwmp", "Level")],
                            adjust: true,
                            sort: "server",
                        },
                        {
                            id: "timeout",
                            header: [tr("cwmp", "Timeout")],
                            adjust: true,
                            sort: "server",
                        },
                        {
                            id: "exec_time", adjust: true,
                            header: [tr("cwmp", "Exec Time")],

                            sort: "server",
                        },
                        {
                            id: "resp_time", adjust: true,
                            header: [tr("cwmp", "Resp Time")],
                            sort: "server",
                        },
                        {id: "none", header: [""], fallspace: true},
                        // { header: { content: "headerMenu" }, headermenu: false, width: 35 }
                    ],
                    leftSplit: 1,
                    rightSplit: 0,
                    pager: true,
                    onClick: {
                        "do_log_detail": function (e, id, node) {
                            showLog(id, node)
                        }
                    }
                }),
                wxui.getTableFooterBar({
                    tableid: tableid,
                    callback: reloadData,
                    actions: [],
                }),
            ]
        })
    })
</script>
</body>

</html>